<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
    :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="运营公司" prop="companyName">
            <el-input v-model="form.companyName" placeholder="请输入运营公司名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="路段名称" prop="roadName">
            <el-input v-model="form.roadName" placeholder="请输入路段名称"/>
          </el-form-item>
        </el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="路段编号" prop="roadCode">
						<el-input v-model="form.roadCode" placeholder="请输入路段编号"/>
					</el-form-item>
				</el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="收费站名称" prop="stationName">
            <el-input v-model="form.stationName" placeholder="请输入收费站名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="收费站编码" prop="stationCode">
            <el-input v-model="form.stationCode" placeholder="请输入收费站编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="现有站级服务器是否主备部署" prop="primaryBackupServer">
            <el-select v-model="form.primaryBackupServer" placeholder="请选择现有站级服务器是否主备部署，可选值：是/否">
              <el-option label="请选择" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="车道名称" prop="laneName">
            <el-input v-model="form.laneName" placeholder="请输入车道名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="车道编号" prop="laneCode">
            <el-input v-model="form.laneCode" placeholder="请输入车道编号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="出入口" prop="laneExitEntrance">
            <el-select v-model="form.laneExitEntrance" placeholder="请选择车道是出口还是入口：出口/入口">
              <el-option label="请选择" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="物理车道编号" prop="laneNo">
            <el-input-number :min="1" :max="1000" v-model="form.laneNo" placeholder="请输入物理车道编号，数字"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="车道类型" prop="laneType">
            <el-select v-model="form.laneType" placeholder="请选择车道类型：ETC/混合/自助卡机/虚拟">
              <el-option label="请选择" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设备类型" prop="deviceType">
            <el-input v-model="form.deviceType" placeholder="请输入设备类型"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设备品牌" prop="deviceBrand">
            <el-input v-model="form.deviceBrand" placeholder="请输入设备品牌"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设备规格" prop="deviceSpecs">
            <el-input v-model="form.deviceSpecs" placeholder="请输入设备规格"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设备IP" prop="deviceIp">
            <el-input v-model="form.deviceIp" placeholder="请输入设备IP"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设备CPU" prop="deviceCpu">
            <el-input v-model="form.deviceCpu" placeholder="请输入设备CPU"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设备内存" prop="deviceMemory">
            <el-input v-model="form.deviceMemory" placeholder="请输入设备内存描述"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="TpStationInfoDialog">
// ========== 1. 导入语句 ==========
import { useDict } from '/@/hooks/dict';
import { rule } from '/@/utils/validate';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/device/tpStationInfo';

// ========== 2. 组件定义 ==========
// 定义组件事件
const emit = defineEmits(['refresh']);

// ========== 3. 响应式数据定义 ==========
// 基础响应式变量
const dataFormRef = ref(); // 表单引用
const visible = ref(false); // 弹窗显示状态
const loading = ref(false); // 加载状态

// 表单数据对象
const form = reactive({
  id: '', // 主键
  companyName: '', // 运营公司名称
  roadName: '', // 路段名称
  stationName: '', // 收费站名称
  stationCode: '', // 收费站编码
  primaryBackupServer: '', // 现有站级服务器是否主备部署，可选值：是/否
  laneName: '', // 车道名称
  laneCode: '', // 车道编号
  laneExitEntrance: '', // 车道是出口还是入口：出口/入口
  laneNo: 0, // 物理车道编号，数字
  laneType: '', // 车道类型：ETC/混合/自助卡机/虚拟
  deviceType: '', // 设备类型
  deviceBrand: '', // 设备品牌
  deviceSpecs: '', // 设备规格
  deviceIp: '', // 设备IP
  roadCode: '', // 路段编号
  deviceCpu: '', // 设备CPU
  deviceMemory: '', // 设备内存描述
});

// ========== 4. 字典数据处理 ==========

// ========== 5. 表单校验规则 ==========
const dataRules = ref({
  companyName: [
    { required: true, message: '运营公司名称不能为空', trigger: 'blur' }
  ],
  roadName: [
    { required: true, message: '路段名称不能为空', trigger: 'blur' }
  ],
  stationName: [
    { required: true, message: '收费站名称不能为空', trigger: 'blur' }
  ],
  stationCode: [
    { required: true, message: '收费站编码不能为空', trigger: 'blur' }
  ],
  primaryBackupServer: [
    { required: true, message: '现有站级服务器是否主备部署，可选值：是/否不能为空', trigger: 'blur' }
  ],
  laneName: [
    { required: true, message: '车道名称不能为空', trigger: 'blur' }
  ],
  laneCode: [
    { required: true, message: '车道编号不能为空', trigger: 'blur' }
  ],
  laneExitEntrance: [
    { required: true, message: '车道是出口还是入口：出口/入口不能为空', trigger: 'blur' }
  ],
  laneNo: [
    { required: true, message: '物理车道编号，数字不能为空', trigger: 'blur' },
    { validator: rule.number, trigger: 'blur' }
  ],
  laneType: [
    { required: true, message: '车道类型：ETC/混合/自助卡机/虚拟不能为空', trigger: 'blur' }
  ],
  deviceType: [
    { required: true, message: '设备类型不能为空', trigger: 'blur' }
  ],
});

// ========== 6. 方法定义 ==========
// 获取详情数据
const getTpStationInfoData = async (id: string) => {
  try {
    loading.value = true;
    const { data } = await getObj({ id: id });
    // 直接将第一条数据赋值给表单
    Object.assign(form, data[0]);
  } catch (error) {
    useMessage().error('获取数据失败');
  } finally {
    loading.value = false;
  }
};

// 打开弹窗方法
const openDialog = (id: string) => {
  visible.value = true;
  form.id = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取TpStationInfo信息
  if (id) {
    form.id = id;
    getTpStationInfoData(id);
  }
};

// 提交表单方法
const onSubmit = async () => {
  loading.value = true; // 防止重复提交

  // 表单校验
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) {
    loading.value = false;
    return false;
  }

  try {
    // 根据是否有ID判断是新增还是修改
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh'); // 通知父组件刷新列表
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// ========== 7. 对外暴露 ==========
// 暴露方法给父组件
defineExpose({
  openDialog
});
</script>
